/**
 * For Cache issue
 *
 * @author ppkwii@gmail.com (Liu Yu Ting)
 */

package database;

//import DatastoreCounter;
//import DatastoreCounterShard;
//import PMF;
import java.util.Map;


import java.util.Collections;
//import java.util.List;
//import java.util.Random;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import java.util.Collections;
//import net.sf.jsr107.Cache;
//import net.sf.jsr107.CacheException;
//import net.sf.jsr107.CacheManager;


//import javax.jdo.PersistenceManager;
//import javax.jdo.Query;

public class NewCache {

	  private String cacheName;
	  private Cache cache;
	  private static Mcache Mcachetmp ;
	    
	  public NewCache(/*String stockno,String sSeason,int TotalScore,int[] TenScore*/) {

	    /*try {
	      cache = CacheManager.getInstance().getCacheFactory().createCache(
	          Collections.emptyMap());
	    } catch (CacheException e) {
	      // It's ok if we can't initialize our cache object since we can fall
	      // back to querying the datastore.
	    	System.out.println("Cache initialization error! ");		    	
	    }*/
		  Map<String, Object> props = Collections.emptyMap();
		    try {
		      //return
		      cache = CacheManager.getInstance().getCacheFactory().createCache(props);
		    } catch (CacheException ex) {
		    	System.out.println("Cache initialization error! ");
		    }    
	  }

	  //public String getCacheName() {  
	  //  return cacheName;
	  //}	  

	  public int setMcache(String stockno,String sSeason,int TotalScore,int[] TenScore){
		  
		  //ShardedCounter counter = new ShardedCounter(name);
		  Mcache Mcache1 = new Mcache(stockno,sSeason,TotalScore, TenScore);
		  		  
		  int itmp = Mcache1.GetMTotalScore();
		  System.out.println("Mcache1 Total Score= "+ itmp);
		  
		  if (cache != null) {
		      cache.put(stockno, Mcache1);
			  return 1 ;
		    }else{
		      return 0;
		    }		  

	  }
	  
      public  Mcache getMcache(String stockno){
    	  	  
    	   Mcache Mcachetmp1 =(Mcache)cache.get(stockno); 
    	  if(Mcachetmp1 != null){ 
    	   return Mcachetmp1 ;
    	  }else{
    	   return null ;	  
    	  }
      }
      
      public int getMTotalSocre(String stockno){
    	  int itmp ;
    	  
    	  Mcache Mcachetmp1 =(Mcache)cache.get(stockno);
    	  
    	  if(Mcachetmp1 != null){ 
    	   itmp = Mcachetmp1.GetMTotalScore();   	  
    	   return itmp;
    	  }else{
    	   return 0 ;	  
    	  }
      }

      int[] getMScore(String stockno){
    	  int[] TenScore = new int[10] ;
    	  Mcache Mcachetmp1 =(Mcache)cache.get(stockno);
    	  
    	  
    	  return TenScore ;
      }
      
      
	  public boolean isInDatastore() {
	    boolean counterStored = false;
	//    PersistenceManager pm = PMF.get().getPersistenceManager();
	    try {
	  //    if (getThisCounter(pm) != null) {
	        counterStored = true;
	   //   }
	    } finally {
//	      pm.close();
	    }
	    return counterStored;
	  }
	  
	 
/*
 *  private Cache createCache() throws ServletException {
    Map<String, Object> props = Collections.emptyMap();
    try {
      return CacheManager.getInstance().getCacheFactory().createCache(props);
    } catch (CacheException ex) {
      throw new ServletException("Could not initialize cache:", ex);
    }
  }
 * */
	  
	  
	  
/*
	  public int getCount() {
	    if (cache != null) {
	      Integer cachedCount = (Integer) cache.get("count" + cacheName);
	      if (cachedCount != null) {
	        return cachedCount.intValue();
	      } 
	    }

	    int sum = 0;
	    PersistenceManager pm = PMF.get().getPersistenceManager();

	    try {
	      Query shardsQuery = pm.newQuery(DatastoreCounterShard.class, 
	                                      "counterName == nameParam");
	      shardsQuery.declareParameters("String nameParam");
	      List<DatastoreCounterShard> shards = 
	          (List<DatastoreCounterShard>) shardsQuery.execute(counterName);
	      if (shards != null && !shards.isEmpty()) {
	        for (DatastoreCounterShard current : shards) {
	          sum += current.getCount();
	        }
	      }
	    } finally {
	      pm.close();
	    }

	    // Update the cache with the count we just found.
	    if (cache != null) {
	      cache.put("count" + counterName, new Integer(sum));
	    }

	    return sum;
	  }
*/


/*
	  public void increment(int count) {
	    if (cache != null) {
	      Integer cachedCount = (Integer) cache.get("count" + counterName);
	      if (cachedCount != null) {
	        cache.put("count" + counterName, 
	            new Integer(count + cachedCount.intValue()));
	      } 
	    }

	    // Find how many shards are in this counter.
	    int shardCount = 0;
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    try {
	      DatastoreCounter current = getThisCounter(pm);
	      shardCount = current.getShardCount();
	    } finally {
	      pm.close();
	    }

	    // Choose the shard randomly from the available shards.
	    Random generator = new Random();
	    int shardNum = generator.nextInt(shardCount);
	    
	    pm = PMF.get().getPersistenceManager();
	    try {
	      Query randomShardQuery = pm.newQuery(DatastoreCounterShard.class);
	      randomShardQuery.setFilter(
	          "counterName == nameParam && shardNumber == numParam");
	      randomShardQuery.declareParameters("String nameParam, int numParam");
	      List<DatastoreCounterShard> shards = 
	          (List<DatastoreCounterShard>) randomShardQuery.execute(
	              counterName, shardNum);
	      if (shards != null && !shards.isEmpty()) {
	        DatastoreCounterShard shard = shards.get(0);
	        shard.increment(count);
	        pm.makePersistent(shard);
	      } 
	    } finally {
	      pm.close();
	    }
	  }
*/	
	
}

//Cache cache =null ; //= new HashMap();    
//List<String> ListScore= new ArrayList<String>();  
//Map props = new HashMap();
//props.put(GCacheFactory.EXPIRATION_DELTA, 3600);
//  final Cache cache ;
//HashMap<String, String> KeyValue = new HashMap();